import axios from 'axios';
import { web3, walletListContract } from '../../../../config';
import { Message } from '@alifd/next';
import * as constants from './constants';

const changeDetail = (data) => ({
  type: constants.GET_WORKCHECKDATA,
  workCheckData: data,
});

const passWork = () => ({
  type: constants.WORK_PASS,
  dialogVisible: false
});

const rejectWork = () => ({
  type: constants.WORK_REJECT,
  dialogVisible: false
});

// http://localhost:9080/work/search/checking?start=0&length=10
export const getWorkCheckData = () => {
  // console.log('111');
  return (dispatch) => {
    axios.get('http://localhost:9080/work/search/checking?start=0&length=10',{
      headers: {
        'Authorization': "Bearer " + localStorage.getItem("access_token")
      }
    }).then((res) => {
      console.log(res.data.data);
      dispatch(changeDetail(res.data.data));
    }).catch(()=>{
      console.log('error');
    })
  }
}

// dialog open
export const onOpen = (record) => ({
  type: constants.OPEN_DIALOG,
  dialogVisible: true,
  dialogData: record,
  dialogTags: record.tags
});

// dialog close
export const onClose = () => ({
  type: constants.CLOSE_DIALOG,
  dialogVisible: false
});

// checked part 上链 以及 将上链后的address写入数据库并将checked置为1
export const onPass = async (_authorId, _workId, _workName) => {
  // 上链
  Message.show({
    type: 'loading',
    content: '作品上链中，请等待片刻'
  });
  const [adminWallet] = await web3.eth.getAccounts();
  await walletListContract.methods.createWork(adminWallet, _authorId, _workId, _workName).send({
    from: adminWallet
  });
  const workAddress = await walletListContract.methods.getLatestWorkAddress().call();
  console.log(workAddress);
  
  return (dispatch) => {
    axios.get('http://localhost:9080/work/update-admin?checked=1&workId='+workId+'&certAddress='+workAddress, {
      headers: {
        'Authorization': "Bearer " + localStorage.getItem("access_token")
      }
    }).then((res) => {
      alert('操作完成！');
      dispatch(passWork());
    }).catch(() => {
      alert('error');
    });
  }
};

export const onReject = (workId) => {
  return (dispatch) => {
    axios.get('http://localhost:9080/work/update-admin?checked=-1&workId='+workId, {
      headers: {
        'Authorization': "Bearer " + localStorage.getItem("access_token")
      }
    }).then((res) => {
      alert('操作完成！');
      dispatch(rejectWork());
    }).catch(() => {
      alert('error');
    });
  }
};


// search part
export const searchFormChange = (value) => ({
  type: constants.SEARCHFORM_CHANGE,
  searchId: value.id
});

export const searchUserById = (id) => {
  return (dispatch) => {
    axios.get('http://localhost:9080/users/'+id, {
      headers: {
        'Authorization': "Bearer " + localStorage.getItem("access_token")
      }
    }).then((res) => {
      if(res.data == '') {
        alert("未搜索到结果");
      }else {
        console.log(res.data);
        let newData = [];
        newData.push(res.data);
        dispatch(changeDetail(newData));
      }
    }).catch(() => {
      console.log('error');
    })
  }
};

// enabled part
export const changeEnabled = (checked, id) => {
  let checkedNum = checked ? 1 : 0;
  return () => {
    axios.post('http://localhost:9080/users/'+id+'/'+checkedNum, {
      headers: {
        'Authorization': "Bearer " + localStorage.getItem("access_token")
      }
    }).then((res) => {
      alert('设置成功！');
    }).catch(() => {
      alert('error');
    })
  }
};